

export default class Store {

  static initMap: StoreInitMap
  static _ins: Store
  static get instance(): Store {

    return this._ins ? this._ins : new Store;
  }
  constructor() {
    if (!Store.initMap) {
      throw ('please init StoreMap first, thanks!')
    } else {
      this.map = Object.assign(JSON.parse(localStorage.getItem(this.mapName) || "{}"), Store.initMap)
      Store._ins = this
    }
  }

  moduleName = 'Store'
  public prefix: string = STORE_PREFIX
  public mapName = this.prefix + '-map'
  public prefixSize: number = STORE_PREFIX_SIZE
  public map: {
    activityID: number
    fbRedirectUrl: string
    fbId: string | number
    version: string

    userInfo: UserInfo
    zoneInfo: ZoneInfo
    roleInfo: RoleInfo
  }

  setItem(key, value) {
    this.map[key] = value
    localStorage.setItem(this.mapName, JSON.stringify(this.map))
  }

  getItem(key) {
    return this.map[key] || null
  }

  clear() {
    localStorage.removeItem(this.mapName)
  }

}
